<?php
namespace app\job;

use app\common\model\Bill;
use app\common\model\Config;
use app\common\model\Order;
use fast\Nuo;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;
use think\queue\Job;

class RedBill extends BaseJob
{

    /**
     * 红冲发票快捷申请
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        Log::info('红冲发票开票申请开始');
        Log::info($data);
        Db::startTrans();
        try{
            $billInfo = Bill::where(['id'=>$data['bill_id'],'client_status'=>4])->find();//状态为作废中的发票才可以申请
            if ($billInfo){
                $bill_info = Bill::where(['parent_id'=>$data['bill_id']])->find();
                if (!$bill_info){
                    $bill_info = Bill::create([
                        'buyerName'=>$billInfo->buyerName,
                        'type'=>$billInfo->type,
                        'bill_type'=>2,
                        'buyerTaxNum'=>$billInfo->buyerTaxNum,
                        'bill_money'=>(-$billInfo->bill_money),
                        'email'=>$billInfo->email,
                        'mobile'=>$billInfo->mobile,
                        'openBlank'=>$billInfo->openBlank,
                        'buyerAccount'=>$billInfo->buyerAccount,
                        'buyerAddress'=>$billInfo->buyerAddress,
                        'buyerTel'=>$billInfo->buyerTel,
                        'member_id'=>$billInfo->member_id,
                        'status'=>20,
                        'orderNo'=>getOrderSn(),
                        'parent_id'=>$data['bill_id'],
                    ]);
                }
                $nuo = new Nuo();
                $arr = $nuo->fastInvoiceRed([
                    'orderNo'=>$bill_info->orderNo,
                    'taxNum'=>Env::get('nuonuo.Taxnum'),
                    'billNo'=>$data['billNo'],//第二步返回的 红字确认单编号
                    'billUuid'=>$data['billUuid'],//第二步返回的 红字确认单uuid
                    'invoiceLine'=>'pc',
                    'callBackUrl'=>'https://xm.xmlhzx.cn/index/callback/red_nuo',
                ]);
                if ($arr['code'] == '0'){
                    throw new Exception('第三方开票接口请求失败');
                }elseif ($arr['code'] == 'E0000'){//开票成功
                    $bill_info->invoiceSerialNum = $arr['result']['invoiceSerialNum'];
                    $bill_info->status = 20;
                    $bill_info->save();
                }elseif ($arr['code'] == 'E9041'){//确认单为已开具状态时不支持冲红
                    $billInfo->invoiceSerialNum = $arr['result']['invoiceSerialNum'];
                    $billInfo->status = 20;
                    $billInfo->save();
                }else{
                    $billInfo->status = 22;
                    $billInfo->reason = $arr['describe'];
                    $billInfo->save();
                }
            }
            Db::commit();
            Log::info('开票成功结束');
        }catch (Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            Log::info('开票失败结束');
            throw $e;
        }
    }









}